/*
	Project/Purpose:
		Timer

	Description:
		A simple timer.

	Notes:
		usage:
			// Create a new timer, pass true to autostart if desired
			var myTimer = new Timer([bool autoStart]); 

			// Start if autostarted
			myTimer.start()

			// Stop if desired
			myTimer.stop()

			// Return milliseconds since start.
			// If called before stop, it will give you a live count
			// If called after stop it will give you a the final, full run time.
			myTimer.getDuration()

			// Returns "X minutes X seconds X milliseconds" based on the value
			// of getDuration()
			myTimer.getDurationFormatted()

			// Reset the timer to zero.
			myTimer.clear()

			// Reset the timer to zero, auto starts timer.
			myTimer.reset();

			// Get the Start Time (milliseconds since Unix epoch)
			myTimer.getStartTime()

			// Get Stop Time (milliseconds since Unix epoch)
			myTimer.getStopTime()

			// Get Error string.
			myTimer.getError()

	Last Updated for Release:
		WCS7

	$Last Updated: 10-Jan-2012 8:30:29AM  $

*/

function jjTimer( autoStart ) {

	var startTime = 0, 
		stopTime = 0,
		isStarted = false,
		duration = 0,
		error = '';

	// Private
	function _getCurrentTime() { return +new Date() }
	
	function _start(){
		if (isStarted !== false) {
			error = 'Timer Already Started.';
			return false;
		} else {
			startTime = _getCurrentTime();
			isStarted = true;
			return true;
		}
	}

	function _stop(){
		if (isStarted !== true) {
			error = 'Timer Not Started.';
			return false;
		} else if (stopTime !== 0) {
			error = 'Timer Already Stopped.';
			return false;
		} else {
			stopTime = _getCurrentTime();
			duration = stopTime - startTime;
			return true;
		}
	}

	function _clear(){
		startTime = 0;
		stopTime = 0;
		isStarted = false;
		duration = 0;
		error = '';
	}

	function _reset(){
		_clear.call(this);
		_start.call(this);
	}

	function _getDuration(){
		if (isStarted !== true) {
			error = 'Timer Not Started.';
			return false;
		} else if (stopTime == 0) {
			// Timer has not stopped, so get live time
			return (_getCurrentTime() - startTime);
		} else {
			return duration;
		}
	}

	function _getDurationFormatted(){

		var tS, h, m, s, mi;
		// constants
		var M=60000, S=1000;

		if (mi = _getDuration.call(this)) {
			m = Math.floor(mi/M);
			mi = mi%M;
			s = Math.floor(mi/S);
			mi = mi%S;
			return m+" minutes "+s+" seconds "+mi+" milliseconds";
		} else {
			return false;
		}
	}

	function _getStartTime() { return startTime; }
	function _getStopTime() { return stopTime; }
	function _getError() { return error; }

	// Auto Starter
	if (autoStart) { _start.call(this); }

	// Public Privleged
	this.start = function start(){ return _start.call(this); }
	this.stop = function stop(){ return _stop.call(this); }
	this.clear = function clear(){ return _clear.call(this); }
	this.reset = function reset(){ return _reset.call(this); }

	this.getStartTime = function getStartTime(){ return _getStartTime.call(this); }
	this.getStopTime = function getStopTime(){ return _getStopTime.call(this); }
	this.getError = function getError(){ return _getError.call(this); }
	this.getDuration = function getDuration() { return _getDuration.call(this); }
	this.getDurationFormatted = function getDurationFormatted() { return _getDurationFormatted.call(this); }
}